using System;
using System.Collections.Generic;
using System.IO;

namespace Gloom
{
	public static class GLSpecParser
	{
		private static StreamWriter debugLog;
		private static StreamWriter failLog;
		
		public static void Parse(List<List<string>> blocks)
		{
			debugLog = openForWrite(MainClass.LimeSourcePath + "GL_translation_debug.txt");
			failLog = openForWrite(MainClass.LimeSourcePath + "GL_translation_errors.txt");
			debugLog.AutoFlush = true;
			failLog.AutoFlush = true;
			
			List<string> glExternFunctions = new List<string>();
			List<string> glManagedFunctions = new List<string>();
			
			StreamWriter externFile = new StreamWriter(File.Open(MainClass.LimeSourcePath + "GL.Extern.cs", FileMode.Create));
			externFile.WriteLine("using System;\nusing System.Runtime.InteropServices;\n\n");
			externFile.WriteLine("namespace Lime\n{\n\tpublic static partial class GL\n\t{");
			
			StreamWriter managedFile = new StreamWriter(File.Open(MainClass.LimeSourcePath + "GL.cs", FileMode.Create));
			managedFile.WriteLine("using System;\nusing System.Runtime.InteropServices;\n\nnamespace Lime\n{\n\tpublic static partial class GL\n\t{");
			
			foreach (List<string> block in blocks)
			{
				UntranslatedGLFunc ugf = UntranslatedGLFunc.Parse(block, debugLog, failLog);
				debugLog.WriteLine("UNTRANSLATED");
				debugLog.WriteLine(ugf.ToString());
				
				if (ugf.Name == "VideoCaptureNV")
				{
					debugLog.WriteLine("Skipping over VideoCaptureNV.");
					continue;
				}
				
				TranslatedUnmanagedGLFunc tugf;
				bool b = TranslatedUnmanagedGLFunc.TryTranslate(ugf, out tugf, debugLog, failLog);
				if (b)
				{
					debugLog.WriteLine("TRANSLATED UNMANAGED");
					debugLog.WriteLine(tugf.ToString());
					
					externFile.WriteLine(tugf.ToString());
				}
				else
				{
					Console.WriteLine("TRANSLATED UNMANAGED conversion failure.");
					failLog.WriteLine("Translated Unmanaged conversion for function `" + ugf.Name + "' just FAILED.\n");
					debugLog.WriteLine("Translated Unmanaged conversion for function `" + ugf.Name + "' just FAILED.\n");
					
					continue;
				}
				
				TranslatedManagedGLFunc tmgf;
				bool b2 = TranslatedManagedGLFunc.TryTranslate(ugf, tugf, out tmgf, debugLog, failLog);
				if (b2)
				{
					debugLog.WriteLine("TRANSLATED MANAGED");
					debugLog.WriteLine(tmgf.ToString());
					
					managedFile.WriteLine(tmgf.ToString());
				}
				else
				{
					Console.WriteLine("TRANSLATED MANAGED conversion failure.");
					failLog.WriteLine("Translated Managed conversion for function `" + ugf.Name + "' just FAILED.\n");
					debugLog.WriteLine("Translated Managed conversion for function `" + ugf.Name + "' just FAILED.\n");
				}
				
				//Console.ReadLine();
			}
			
			managedFile.WriteLine("\t}\n}");
			managedFile.Flush();
			managedFile.Close();
			
			externFile.WriteLine("\t}\n}");
			externFile.Flush();
			externFile.Close();
			
			failLog.Flush();
			failLog.Close();
			debugLog.Flush();
			debugLog.Close();
		}
		
		private static StreamWriter openForWrite(string filename)
		{
			return new StreamWriter(File.Open(filename, FileMode.Create)); // truncates if exists already, creates if doesn't
		}
	}
}
